﻿namespace MyAbpCodeArtCore.Web.Tests
{
    using System;
    using Abp.AspNetCore;
    using Abp.AspNetCore.TestBase;
    using Abp.Dependency;
    using Castle.MicroKernel.Registration;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using FFF.Test.DataAccess.Framework;

    public class Startup
    {
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFrameworkInMemoryDatabase();

            services.AddMvc();

            // Configure Abp and Dependency Injection
            return services.AddAbp<MyAbpCodeArtCoreWebTestModule>(options =>
            {
                options.SetupTest();
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            this.UseInMemoryDb(app.ApplicationServices);

            app.UseAbp(); //Initializes ABP framework.

            // app.UseExceptionHandler("/Error");

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}"
                    );
            });
        }

        private void UseInMemoryDb(IServiceProvider serviceProvider)
        {
            var builder = new DbContextOptionsBuilder<MyDbContext>();
            builder.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseInternalServiceProvider(serviceProvider);
            var options = builder.Options;

            var iocManager = serviceProvider.GetRequiredService<IIocManager>();

            iocManager.IocContainer
                .Register(
                    Component.For<DbContextOptions<MyDbContext>>()
                    .Instance(options)
                    .LifestyleSingleton()
                );
        }
    }
}
